// Copyright (c) 2024, the WwMusic project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

package com.springfairy.controller;

import com.springfairy.model.MoodTrack;
import com.springfairy.model.MoodTrackPoints;
import com.springfairy.service.MoodTrackService;
import com.springfairy.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/moodtracks")
public class MoodTrackController {

    @Autowired
    private MoodTrackService moodTrackService;

    /**
     * 创建一条用户轨迹,每一天只能一条轨迹
     * @param track 轨迹
     * @param request 请求
     * @return track
     */
    @PostMapping("/create")
    public ResponseEntity<MoodTrack> createTrack(@RequestBody MoodTrack track, HttpServletRequest request) {
        track.setUserId(UserUtils.getLoginUserId(request));
        MoodTrack created = moodTrackService.createTrack(track);
        return ResponseEntity.ok(created);
    }

    /**
     * 创建自定义轨迹
     * @param moodTrack 轨迹
     */
    @PostMapping("/custom/create")
    public ResponseEntity<MoodTrack> createCustomTrack(@RequestBody MoodTrack moodTrack,HttpServletRequest request) {
        moodTrack.setId(UserUtils.getLoginUserId(request));
        MoodTrack created = moodTrackService.createCustomTrack(moodTrack);
        return ResponseEntity.ok(created);
    }


    /**
     * 获取用户所有轨迹
     * @return 轨迹列表
     */
    @GetMapping("/list")
    public ResponseEntity<List<MoodTrack>> getTracksByUser(HttpServletRequest request) {
        Integer userId = UserUtils.getLoginUserId(request);
        if(userId == null) {
            return ResponseEntity.ok(new ArrayList<>());
        }
        List<MoodTrack> list = moodTrackService.getTracksByUser(userId);
        return ResponseEntity.ok(list);
    }


    /**
     * 添加标记点
     * @param trackId 轨迹ID
     * @param point 标记
     */
    @PostMapping("/points/create")
    public ResponseEntity<MoodTrackPoints> addPoint(@RequestParam Integer trackId,
                                                    @RequestBody MoodTrackPoints point) {
        MoodTrackPoints created = moodTrackService.addTrackPoint(trackId,point);
        return ResponseEntity.ok(created);
    }

    /**
     * 获取某条轨迹所有标记点
     * @param trackId 轨迹ID
     * @return 标记点列表
     */
    @GetMapping("/{trackId}/points")
    public ResponseEntity<List<MoodTrackPoints>> getPointsByTrack(@PathVariable Integer trackId) {
        List<MoodTrackPoints> points = moodTrackService.getPointsByTrack(trackId);
        return ResponseEntity.ok(points);
    }

    /**
     * 删除一个标记点
     * @param pointId 轨迹点ID
     */
    @GetMapping("/points/delete")
    public ResponseEntity<Void> deletePoint(@RequestParam Integer pointId) {
        boolean success = moodTrackService.deletePoint(pointId);
        return success ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }

    /**
     * 更新point
     */
    @PostMapping("/points/update")
    public ResponseEntity<MoodTrackPoints> updatePoint(@RequestBody MoodTrackPoints point) {
        MoodTrackPoints moodTrack1 =  moodTrackService.updatePoint(point);
        return ResponseEntity.ok(moodTrack1);
    }


}

